//
//  CarFilterVC.m
//  New_Runway
//
//  Created by mack on 2018/2/2.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarFilterVC.h"
#import "DeskMeunCell.h"
#import "FilterHeaderView.h"
#import "SelectionValueCell.h"
#import "SelectionAgeCell.h"
#import "FactoryAgeCell.h"
#import "SelectedColorCell.h"
#import "CarTypeCell.h"

#import "ColorModel.h"
#import "CarTypeModel.h"
#import "CarTraderChoose.h"

@interface CarFilterVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *controllers;
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, strong) NSMutableArray *colorsArray;
@property (nonatomic, strong) NSMutableArray *carTypeArray;
@property (nonatomic, strong) NSMutableDictionary *conditionDic;
@property (nonatomic, strong) NSMutableDictionary  *indexPathDic;

@end

@implementation CarFilterVC
{
    
    NSArray *_protectionArray;
    NSString *_showKm;
    NSString *_showAge;
    NSString *_shoewColor;
    NSString *_showType;
    NSString *_showPro;
    NSString *_showFactoryAge;
    NSInteger _threeIndex;
    NSInteger _fourIndex;
    NSInteger _fiveIndex;
    NSString *_showTypeId;
    NSInteger _ageFirstIndex;
    NSInteger _ageEndIndex;
    NSInteger _KmFirstIndex;
    NSInteger _KmEndIndex;
    NSInteger _factoryAgeFirstIndex;
    NSInteger _factoryAgeEndIndex;
    NSString *_shopName;
//    NSString *_carTypeName;

}
-(NSMutableDictionary *)indexPathDic{
    if (!_indexPathDic) {
        _indexPathDic = [[NSMutableDictionary alloc] init];
    }
    return _indexPathDic;
}
-(NSMutableArray *)colorsArray{
    if (!_colorsArray) {
        _colorsArray = [[NSMutableArray alloc] init];
        
    }
    return _colorsArray;
    
}
-(NSMutableArray *)carTypeArray{
    if (!_carTypeArray) {
        _carTypeArray = [[NSMutableArray alloc] init];
    }
    return _carTypeArray;
}
-(NSMutableDictionary *)conditionDic{
    if (!_conditionDic) {
        _conditionDic = [[NSMutableDictionary alloc] init];
    }
    return _conditionDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"筛选";
    [self.navigationBarView.rightItem setTitle:@"确定" forState:UIControlStateNormal];
    [self resetButton];
    self.view.backgroundColor= kColorWhite();
    [self creatCollectionView];
    [self laodCarOutsideColor];
    [self loadCarType];

}
-(void)setArrayValue{
    _protectionArray = @[@"国I",@"国II",@"国III",@"国IV",@"国V",@"欧I",@"欧II",@"欧III",@"欧IV",@"欧V"];
    self.indexPathDic = [self.selectedDic mutableCopy];
    _threeIndex = [[self.indexPathDic objectForKey:@"threeIndex"] integerValue];
    _fourIndex = [[self.indexPathDic objectForKey:@"fourIndex"] integerValue];
    _fiveIndex = [[self.indexPathDic objectForKey:@"fiveIndex"] integerValue];
    _shopName = [self.indexPathDic objectForKey:@"shopName"];
    if (_fourIndex == INTMAX_MAX) {
        _showType = @"";
    }else{
        carTypeResult *info = self.carTypeArray[_fourIndex];
        _showType = info.carTypeName;
    }
    self.conditionDic = [self.allDataDic mutableCopy];
    
    _shoewColor = self.conditionDic[@"carOutColor"];
    _showPro = self.conditionDic[@"carEnviron"];

    NSArray *ageArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"10",@"不限"];
    if ([DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromCarAge"]] && [DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toCarAge"]]) {
        _showAge = @"不限";
        _ageFirstIndex = 0;
        _ageEndIndex = 7;

    }else if ([DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromCarAge"]] &&
              ![DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toCarAge"]]){
        _showAge = [NSString stringWithFormat:@"%@年以内",[self.conditionDic objectForKey:@"toCarAge"]];
        _ageFirstIndex = 0;
        _ageEndIndex = [ageArray indexOfObject:[self.conditionDic objectForKey:@"toCarAge"]];
    }else if (![DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromCarAge"]] &&
              [DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toCarAge"]]){
        _showAge = [NSString stringWithFormat:@"%@年以上",[self.conditionDic objectForKey:@"fromCarAge"]];
        _ageFirstIndex = [ageArray indexOfObject:[self.conditionDic objectForKey:@"fromCarAge"]];
        _ageEndIndex = 7;
    }else{
        _showAge = [NSString stringWithFormat:@"%@ ~ %@年",[self.conditionDic objectForKey:@"fromCarAge"],[self.conditionDic objectForKey:@"toCarAge"]];
        _ageFirstIndex = [ageArray indexOfObject:[self.conditionDic objectForKey:@"fromCarAge"]];
        _ageEndIndex = [ageArray indexOfObject:[self.conditionDic objectForKey:@"toCarAge"]];

    }

    NSArray *KmArray = @[@"0",@"1",@"3",@"6",@"10",@"不限"];
    if ([DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromMileage"]] && [DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toMileage"]]) {
        _showKm = @"不限";
        _KmFirstIndex = 0;
        _KmEndIndex = 5;
        
    }else if ([DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromMileage"]] &&
              ![DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toMileage"]]){
        _showKm = [NSString stringWithFormat:@"%@万公里以内",[self.conditionDic objectForKey:@"toMileage"]];
        _KmFirstIndex = 0;
        _KmEndIndex = [KmArray indexOfObject:[self.conditionDic objectForKey:@"toMileage"]];
    }else if (![DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromMileage"]] &&
              [DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toMileage"]]){
        _showKm = [NSString stringWithFormat:@"%@万公里以上",[self.conditionDic objectForKey:@"fromMileage"]];
        _KmFirstIndex = [KmArray indexOfObject:[self.conditionDic objectForKey:@"fromMileage"]];
        _KmEndIndex = 5;
    }else{
        _showKm = [NSString stringWithFormat:@"%@ ~ %@万公里",[self.conditionDic objectForKey:@"fromMileage"],[self.conditionDic objectForKey:@"toMileage"]];
        _KmFirstIndex = [KmArray indexOfObject:[self.conditionDic objectForKey:@"fromMileage"]];
        _KmEndIndex = [KmArray indexOfObject:[self.conditionDic objectForKey:@"toMileage"]];
        
    }
        NSArray *factoryAgeArray = @[@"0",@"30",@"45",@"60",@"90",@"120",@"不限"];
        if ([DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromCreateTime"]] && [DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toCreateTime"]]) {
        _showFactoryAge = @"不限";
        _factoryAgeFirstIndex = 0;
        _factoryAgeEndIndex = 6;
        
    }else if ([DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromCreateTime"]] &&
              ![DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toCreateTime"]]){
        _showFactoryAge = [NSString stringWithFormat:@"%@天以内",[self.conditionDic objectForKey:@"toCreateTime"]];
        _factoryAgeFirstIndex = 0;
        _factoryAgeEndIndex = [factoryAgeArray indexOfObject:[self.conditionDic objectForKey:@"toCreateTime"]];
    }else if (![DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"fromCreateTime"]] &&
              [DataCheck StringIsNullOrEmpty:[self.conditionDic objectForKey:@"toCreateTime"]]){
        _showFactoryAge = [NSString stringWithFormat:@"%@天以上",[self.conditionDic objectForKey:@"fromCreateTime"]];
        _factoryAgeFirstIndex = [factoryAgeArray indexOfObject:[self.conditionDic objectForKey:@"fromCreateTime"]];
        _factoryAgeEndIndex = 6;
    }else{
        _showFactoryAge = [NSString stringWithFormat:@"%@ ~ %@天",[self.conditionDic objectForKey:@"fromCreateTime"],[self.conditionDic objectForKey:@"toCreateTime"]];
        _factoryAgeFirstIndex = [factoryAgeArray indexOfObject:[self.conditionDic objectForKey:@"fromCreateTime"]];
        _factoryAgeEndIndex = [factoryAgeArray indexOfObject:[self.conditionDic objectForKey:@"toCreateTime"]];
        
    }
   
    
    

}

-(void)resetButton{
    
    UIButton *resetBtn = [[UIButton alloc] init];
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [resetBtn setTitleColor:kColorWhite() forState:UIControlStateNormal];
    [resetBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [self.navigationBarView addSubview:resetBtn];
    kSelfWeak;
    [resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.navigationBarView.rightItem.mas_left).mas_offset(5);
        make.centerY.equalTo(weakSelf.navigationBarView.rightItem);
        make.height.mas_equalTo(35);
    }];
    [resetBtn addTarget:self action:@selector(resetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)resetButtonClick{
    
   NSArray *array = [self.conditionDic allKeys];
    for (NSString *key in array) {
        [self.conditionDic setObject:@"" forKey:key];
    }
    NSArray *indexS = [self.indexPathDic allKeys];
    for (NSString *key in indexS) {
        if ([key isEqualToString:@"shopName"]) {
            [self.indexPathDic setObject:@"" forKey:key];
        }else{
            [self.indexPathDic setObject:@(INTMAX_MAX) forKey:key];

        }
    }
    _showAge = @"不限";
    _ageFirstIndex = 0;
    _ageEndIndex = 7;
    _showKm = @"不限";
    _KmFirstIndex = 0;
    _KmEndIndex = 5;
    _showFactoryAge = @"不限";
    _factoryAgeFirstIndex = 0;
    _factoryAgeEndIndex = 6;
    _shoewColor = @"";
    _showPro = @"";
    _showType = @"";
    _shopName = @"";
   
    
    _threeIndex = _fourIndex = _fiveIndex = INTMAX_MAX;

    [self.collectionview reloadData];
 
}
-(void)creatCollectionView{
    //创建一个layout布局类
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小为100*100
    //    layout.itemSize = CGSizeMake(80, 110);
    layout.minimumLineSpacing      = 0;
    layout.minimumInteritemSpacing = 0;
    //    [layout setHeaderReferenceSize:CGSizeMake(Screen_width, self.view.frame.size.width * 3 / 5)];
    //创建collectionView 通过一个布局策略layout来创建
    _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.navH, Screen_width, Screen_height - self.navH - self.BotH) collectionViewLayout:layout];
    //代理设置
    _collectionview.backgroundColor = kColorWhite();
    _collectionview.delegate=self;
    _collectionview.dataSource=self;
    //注册item类型 这里使用系统的类型
    [_collectionview registerNib:[UINib nibWithNibName:@"DeskMeunCell" bundle:nil] forCellWithReuseIdentifier:@"DeskMeunCell"];
    
    [_collectionview registerClass:[SelectionValueCell class] forCellWithReuseIdentifier:@"SelectionValueCell"];
    [_collectionview registerClass:[SelectionAgeCell class] forCellWithReuseIdentifier:@"SelectionAgeCell"];
    [_collectionview registerClass:[FactoryAgeCell class] forCellWithReuseIdentifier:@"FactoryAgeCell"];
    [_collectionview registerClass:[SelectedColorCell class] forCellWithReuseIdentifier:@"SelectedColorCell"];
    [_collectionview registerClass:[CarTypeCell class] forCellWithReuseIdentifier:@"CarTypeCell"];

    [_collectionview registerClass:[FilterHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterHeaderView"];
    [_collectionview registerClass:[CarTraderChoose class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CarTraderChoose"];

    
    [_collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

    [self.view addSubview:_collectionview];

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (self.isBuyCar == YES) {
        return 5;
    }
    return 6;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    WorkDeskMainModel *sectionModel = self.sectionArray[section];
    
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return self.colorsArray.count;

    }else if (section == 3){
        return self.carTypeArray.count;

    }else if (section == 4){
        
        return _protectionArray.count;

    }
    return 1;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    kSelfWeak;

    if (indexPath.section == 0) {
        SelectionValueCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectionValueCell" forIndexPath:indexPath];
        __weak typeof(cell) weakcell = cell;
        cell.KmBlock = ^(NSString *fromKm, NSString *toKm, int firstPath, int lastPath) {

            if ([fromKm isEqualToString:@""] && [toKm isEqualToString:@"不限"]) {
                fromKm = @"";
                toKm = @"";
                _showKm = @"不限";
                
            }else if (![fromKm isEqualToString:@""] && [toKm isEqualToString:@"不限"]) {
                fromKm = fromKm;
                toKm = @"";
                _showKm = [NSString stringWithFormat:@"%@万公里以上",fromKm];
            }else if (![fromKm isEqualToString:@""]&& ![toKm isEqualToString:@"不限"]) {
                fromKm = fromKm;
                toKm = toKm;
                _showKm = [NSString stringWithFormat:@"%@~%@万公里",fromKm,toKm];
            }else if ([fromKm isEqualToString:@""] && ![toKm isEqualToString:@"不限"]) {
                fromKm = @"";
                toKm = toKm;
                 _showKm = [NSString stringWithFormat:@"%@万公里以内",toKm];
            }
            NSDictionary *dic = @{ @"fromMileage":fromKm,
                                   @"toMileage":toKm
                                   };
            [weakSelf.conditionDic addEntriesFromDictionary:dic];
//            [weakSelf.collectionview reloadSections:[NSIndexSet indexSetWithIndex:0]];
            weakcell.valueL.text = _showKm;
            _KmFirstIndex = firstPath;
            _KmEndIndex = lastPath;
            
        };
        cell.firstIndex = _KmFirstIndex;
        cell.endIndex = _KmEndIndex;
        cell.valueL.text = _showKm;
        return cell;
    }else if (indexPath.section == 1){
        SelectionAgeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectionAgeCell" forIndexPath:indexPath];
      
        __weak typeof(cell) weakcell = cell;
        cell.AgeBlock = ^(NSString *fromAge, NSString *toAge, int firstIndexPath, int lastIndexPath) {
            
            if ([fromAge isEqualToString:@""] && [toAge isEqualToString:@"不限"]) {
                fromAge = @"";
                toAge = @"";
                _showAge = @"不限";
            }else if (![fromAge isEqualToString:@""] && [toAge isEqualToString:@"不限"]) {
                fromAge = fromAge;
                toAge = @"";
                _showAge = [NSString stringWithFormat:@"%@年以上",fromAge];
            }else if (![fromAge isEqualToString:@""]&&![toAge isEqualToString:@"不限"]) {
                fromAge = fromAge;
                toAge = toAge;
                _showAge = [NSString stringWithFormat:@"%@~%@年",fromAge,toAge];

            }else if ([fromAge isEqualToString:@""] && ![toAge isEqualToString:@"不限"]) {
                fromAge = @"";
                toAge = toAge;
                _showAge = [NSString stringWithFormat:@"%@年以下",toAge];

            }
        
            NSDictionary *dic = @{ @"fromCarAge":fromAge,
                                   @"toCarAge":toAge
                                   };
            [weakSelf.conditionDic addEntriesFromDictionary:dic];
            weakcell.valueL.text = _showAge;
            _ageFirstIndex = firstIndexPath;
            _ageEndIndex = lastIndexPath;

        };
        cell.firstIndex = _ageFirstIndex;
        cell.endIndex = _ageEndIndex;
        cell.valueL.text = _showAge;
        return cell;
        
    }else if (indexPath.section == 5){
        FactoryAgeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FactoryAgeCell" forIndexPath:indexPath];
        __weak typeof(cell) weakcell = cell;
        cell.factoryAgeBlock = ^(NSString *fromAge, NSString *toAge, int firstPath, int lastPath) {

            if ([fromAge isEqualToString:@""] && [toAge isEqualToString:@"不限"]) {
                fromAge = @"";
                toAge = @"";
                _showFactoryAge = @"不限";
            }else if (![fromAge isEqualToString:@""] && [toAge isEqualToString:@"不限"]) {
                fromAge = fromAge;
                toAge = @"";
                _showFactoryAge = [NSString stringWithFormat:@"%@天以上",fromAge];
            }else if (![fromAge isEqualToString:@""]&&![toAge isEqualToString:@"不限"]) {
                fromAge = fromAge;
                toAge = toAge;
                _showFactoryAge = [NSString stringWithFormat:@"%@~%@天",fromAge,toAge];

            }else if ([fromAge isEqualToString:@""] && ![toAge isEqualToString:@"不限"]) {
                fromAge = @"";
                toAge = toAge;
                _showFactoryAge = [NSString stringWithFormat:@"%@天以下",toAge];

            }
            NSDictionary *dic = @{ @"fromCreateTime":fromAge,
                                   @"toCreateTime":toAge
                                   };
            [weakSelf.conditionDic addEntriesFromDictionary:dic];
            weakcell.valueL.text = _showFactoryAge;
            _factoryAgeFirstIndex = firstPath;
            _factoryAgeEndIndex = lastPath;
        };
        cell.firstIndex = _factoryAgeFirstIndex;
        cell.endIndex = _factoryAgeEndIndex;
        cell.valueL.text = _showFactoryAge;
        
        return cell;
        
    }else if (indexPath.section == 2){
        ColorResult *info = self.colorsArray[indexPath.row];
        SelectedColorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectedColorCell" forIndexPath:indexPath];
        cell.ColorIamge.backgroundColor = [UIColor colorFromHexCode:info.colorCode];
        cell.titleL.text = info.colorName;
        if (indexPath.row == 1) {
            cell.ColorIamge.layer.borderColor = kColorBlack().CGColor;
            cell.ColorIamge.layer.borderWidth = 0.5;
        }else{
            cell.ColorIamge.layer.borderColor = [UIColor clearColor].CGColor;

        }
        cell.titleL.textColor = MAIN_FONT_COLOR();
        if (_threeIndex == indexPath.row) {
            cell.titleL.textColor = MAIN_NAV_COLOR();
        }

        return cell;
    }else if (indexPath.section == 3){
        carTypeResult *info = self.carTypeArray[indexPath.row];
        CarTypeCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"CarTypeCell" forIndexPath:indexPath];
        cell.titleL.text = info.carTypeName;
        cell.titleL.textColor = MAIN_FONT_COLOR();
        if (_fourIndex == indexPath.row) {
            cell.titleL.textColor = MAIN_NAV_COLOR();
        }
        return cell;
    }else if (indexPath.section == 4){
        CarTypeCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"CarTypeCell" forIndexPath:indexPath];
        cell.titleL.text = _protectionArray[indexPath.row];
        cell.titleL.textColor = MAIN_FONT_COLOR();
        if (_fiveIndex == indexPath.row) {
            cell.titleL.textColor = MAIN_NAV_COLOR();
        }
        return cell;

    }
    return nil;
    
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section

{
    
//    return UIEdgeInsetsMake(10, (Screen_width - 300) / 5, 0, (Screen_width - 300) / 5);//分别为上、左、下、右
    return UIEdgeInsetsMake(0,0,0,0);
    
    
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind ==UICollectionElementKindSectionHeader ) {
        
        if (indexPath.section == 0 || indexPath.section == 1 ) {
            CarTraderChoose *headerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CarTraderChoose" forIndexPath:indexPath];
            headerview.titleL.text = @"车商";
            kSelfWeak;
            headerview.shopIdBlock = ^(NSString *shopId, NSString *shopName) {
                _shopName = shopName;
                [weakSelf.indexPathDic setObject:shopName forKey:@"shopName"];
                [weakSelf.conditionDic setValue:shopId forKey:@"shopId"];
            };
            headerview.textF.text = _shopName;
            return headerview;
            }
     
        NSArray *titles = @[@"里程/单位（万公里）",@"车龄/单位（年）",@"颜色",@"车身类型",@"环保类型",@"库龄/单位（天）",];
        FilterHeaderView *headerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterHeaderView" forIndexPath:indexPath];
        headerview.titleL.text = titles[indexPath.section];
        headerview.valueL.text = @"不限";
        if (indexPath.section == 2) {
            headerview.valueL.text = _shoewColor;
        }else if (indexPath.section == 3){
            headerview.valueL.text = _showType;
        }else if (indexPath.section == 4){
            headerview.valueL.text = _showPro;
        }
        
        return headerview;
     
        
    }else{
      
        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footView.backgroundColor = UIColorHex(0xf5f5f5);
        return footView;
    }
   
    
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 || indexPath.section == 5 || indexPath.section == 1) {
        return CGSizeMake(Screen_width, 120);
    }else if (indexPath.section == 2){
   
        return CGSizeMake((Screen_width - 40) / 4, (Screen_width - 40) / 4  + 30);
    }else if (indexPath.section == 3 || indexPath.section == 4){
         return CGSizeMake((Screen_width - 40) / 3, 50);
    }
    return CGSizeMake(0, 0);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0 ) {
        if (self.isBuyCar == YES) {
            return CGSizeMake(0, 0);

        }else{
            if ([NRWCarUser sharedUser].shopId) {
               
                return CGSizeMake(0, 0);

            }else{
                return CGSizeMake(Screen_width, 60);
            }
        }
    }else if (section == 1){
        return CGSizeMake(0, 0);
    }
    if (self.isBuyCar == NO) {
        if (section == 5){
            return CGSizeMake(0, 0);
        }
    }
    return CGSizeMake(Screen_width, 40);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(Screen_width, 10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 2) {
        _threeIndex = indexPath.row;
        ColorResult *info = self.colorsArray[indexPath.row];
        _shoewColor = info.colorName;
        [self.indexPathDic setObject:@(_threeIndex) forKey:@"threeIndex"];
        [self.conditionDic setObject:_shoewColor forKey:@"carOutColor"];

        [UIView animateWithDuration:0 animations:^{
            [self.collectionview performBatchUpdates:^{
                [self.collectionview reloadSections:[NSIndexSet indexSetWithIndex:2]];
            } completion:nil];
        }];
    }else if (indexPath.section == 3){
        _fourIndex = indexPath.row;
        [self.indexPathDic setObject:@(_fourIndex) forKey:@"fourIndex"];

        carTypeResult *info = self.carTypeArray[indexPath.row];
        _showType = info.carTypeName;
        _showTypeId = [NSString stringWithFormat:@"%zd",info.carTypeId];
        [self.conditionDic setObject:_showTypeId forKey:@"carTypeId"];

        [UIView animateWithDuration:0 animations:^{
            [self.collectionview performBatchUpdates:^{
                [self.collectionview reloadSections:[NSIndexSet indexSetWithIndex:3]];
            } completion:nil];
        }];

    }else if (indexPath.section == 4){
        
        _fiveIndex = indexPath.row;
        [self.indexPathDic setObject:@(_fiveIndex) forKey:@"fiveIndex"];

        _showPro = _protectionArray[indexPath.row];
        [self.conditionDic setObject:_showPro forKey:@"carEnviron"];

        [UIView animateWithDuration:0 animations:^{
            [self.collectionview performBatchUpdates:^{
                [self.collectionview reloadSections:[NSIndexSet indexSetWithIndex:4]];
            } completion:nil];
        }];

    }

    
}
//获取车身颜色
-(void)laodCarOutsideColor{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,ColorApi];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        ColorModel *model = [ColorModel mj_objectWithKeyValues:response];
        if (model.code == 1) {
            [self.colorsArray removeAllObjects];
            for (NSDictionary *dic  in model.result) {
                ColorResult *info = [ColorResult mj_objectWithKeyValues:dic];
                [self.colorsArray addObject:info];
            }
            
            [self.collectionview reloadData];
        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}

//车身类型
-(void)loadCarType{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,carTypeApi];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CarTypeModel *model = [CarTypeModel mj_objectWithKeyValues:response];
        if (model.code == 1) {
            [self.carTypeArray removeAllObjects];
            for (NSDictionary *dic  in model.result) {
                carTypeResult *info = [carTypeResult mj_objectWithKeyValues:dic];
                [self.carTypeArray addObject:info];
            }
            [self setArrayValue];
            [self.collectionview reloadData];
        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)rightItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    self.searchBlock(self.conditionDic,self.indexPathDic);
    
}


@end
